//
//  PPMainViewBadgeCell.m
//  pickpic
//
//  Created by Jinxiao on 8/25/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPHomeDashboardViewBadgeCell.h"

@interface PPHomeDashboardViewBadgeCell ()
@property (readwrite, nonatomic, strong) UIImageView *imageView;
@property (readwrite, nonatomic, strong) UILabel *label;

@property (readwrite, nonatomic, strong) UILabel *numberLabel;
@property (readwrite, nonatomic, strong) UIView *dotView;
@end



@implementation PPHomeDashboardViewBadgeCell


- (instancetype)init {
    self = [super init];

    _imageView = [[UIImageView alloc] init];
    _imageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_imageView];

    _label = [[UILabel alloc] init];
    _label.font = [UIFont systemFontOfSize:17];
    _label.textColor = [UIColor whiteColor];
    [self addSubview:_label];

    _numberLabel = [[UILabel alloc] init];
    _numberLabel.hidden = YES;
    _numberLabel.textAlignment = NSTextAlignmentCenter;
    _numberLabel.font = [UIFont systemFontOfSize:12];
    _numberLabel.textColor = [UIColor whiteColor];
    _numberLabel.backgroundColor = [UIColor colorWithRGBHex:0xff3a2f];
    _numberLabel.layer.cornerRadius = 9;
    _numberLabel.layer.masksToBounds = YES;
    [self addSubview:_numberLabel];

    _dotView = [[UIView alloc] init];
    _dotView.hidden = YES;
    _dotView.backgroundColor = [UIColor colorWithRGBHex:0xff3a2f];
    _dotView.layer.cornerRadius = 5;
    _dotView.layer.masksToBounds = YES;
    [self addSubview:_dotView];

    //    self.clipsToBounds = NO;

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    UIImage *icon = nil;

    switch(_type) {
        case PPMainViewBadgeTypeHome:
            icon = [UIImage imageNamed:@"icon-home"];
            break;

        case PPMainViewBadgeTypeFriend:
            icon = [UIImage imageNamed:@"icon-friend"];
            break;

        case PPMainViewBadgeTypeMiao:
            icon = [UIImage imageNamed:@"icon-miao"];
            break;

        case PPMainViewBadgeTypeFound:
            icon = [UIImage imageNamed:@"icon-found"];
            break;

        case PPMainViewBadgeTypeFavour:
            icon = [UIImage imageNamed:@"icon-collect"];
            break;

        case PPMainViewBadgeTypeContact:
            icon = [UIImage imageNamed:@"icon-address-list"];
            break;

        case PPMainViewBadgeTypeSetting:
            icon = [UIImage imageNamed:@"icon-set"];
            break;
    }

    if(_summary != nil && _summary.count.integerValue > 0) {

        if(_summary.iconURLString.length > 0) {
            [_summary.iconURLString applyToImageView:_imageView placeholder:icon];
        } else {
            if (_summary.type == PPUnreadMessageSummaryTypeFound)
            {
                _imageView.image = [UIImage imageNamed:@"system_message_icon_medium"];
            }
            else
            {
                _imageView.image = [UIImage imageNamed:@"icon-person-150"];
            }
        }
    } else {
        _imageView.image = icon;
    }

    switch(_type) {
        case PPMainViewBadgeTypeHome:
            _label.text = @"主页";
            break;

        case PPMainViewBadgeTypeFriend:
            _label.text = @"朋友";
            break;

        case PPMainViewBadgeTypeMiao:
            _label.text = @"妙人";
            break;

        case PPMainViewBadgeTypeFound:
            _label.text = @"发现";
            break;

        case PPMainViewBadgeTypeFavour:
            _label.text = @"收藏";
            break;

        case PPMainViewBadgeTypeContact:
            _label.text = @"通讯录";
            break;

        case PPMainViewBadgeTypeSetting:
            _label.text = @"设置";
            break;
    }

    [_label sizeToFit];

    _imageView.width = _imageView.height = 76;
    _imageView.layer.cornerRadius = 38;
    _imageView.layer.masksToBounds = YES;

    CGFloat height = _imageView.height + 12 + _label.height;
    _imageView.top = (self.height - height)/2;
    _imageView.centerX = self.width/2;

    _label.bottom = (self.height + height)/2;
    _label.centerX = self.width/2;

    _dotView.width = 10;
    _dotView.height = 10;
    _dotView.top = _imageView.top;
    _dotView.centerX = _imageView.right;

    _numberLabel.left = _imageView.right;
    _numberLabel.top = _imageView.top;
}

- (void)setType:(PPMainViewBadgeType)type {
    _type = type;

    [self setNeedsLayout];
}

- (void)setBadge:(id)badge {
    if([badge isKindOfClass:[NSNumber class]]) {
        _numberLabel.text = [NSString stringWithFormat:@"%li", [badge  integerValue]];
        [_numberLabel sizeToFit];
        _numberLabel.width += 12;
        _numberLabel.height = 18;

        _numberLabel.hidden = NO;
        _dotView.hidden = YES;
    } else if(badge != nil) {
        _dotView.hidden = NO;
        _numberLabel.hidden = YES;
    } else {
        _dotView.hidden = YES;
        _numberLabel.hidden = YES;
    }

    [self setNeedsLayout];
}

- (void)setSummary:(PPUnreadMessageSummaryObject *)summary {
    _summary = summary;

    if(summary == nil || summary.count.integerValue == 0) {
        self.badge = nil;
    } else {
        if(self.type == PPMainViewBadgeTypeHome || self.type == PPMainViewBadgeTypeFound) {
            self.badge = summary.count;
        } else {
            self.badge = [NSNull null];
        }
    }

    [self setNeedsLayout];
}

@end
